home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / BCCollec / Structs / Queues / BCQue.cpp next >
Encoding:
Text File  |  1994-04-21  |  3.3 KB  |  147 lines  |  [TEXT/MPS ]

  1. //  The C++ Booch Components (Version 2.1)
  2. //  (C) Copyright 1990-1993 Grady Booch. All Rights Reserved.
  3. //
  4. //  Restricted Rights Legend
  5. //  Use, duplication, or disclosure is subject to restrictions as set forth 
  6. //  in subdivision (c)(1)(ii) of the Rights in Technical Data and Computer 
  7. //  Software clause at DFARS 252.227-7013. 
  8. //
  9. //  BCQue.cpp
  10. //
  11. //  This file contains the definitions for the queue abstract base class
  12. //  and its iterators.
  13.  
  14. #include "BCQue.h"
  15.  
  16. template<class Item>
  17. BC_TQueue<Item>::BC_TQueue() {}
  18.  
  19. template<class Item>
  20. BC_TQueue<Item>::BC_TQueue(const BC_TQueue<Item>&) {}
  21.  
  22. template<class Item>
  23. BC_TQueue<Item>::~BC_TQueue() {}
  24.  
  25. template<class Item>
  26. BC_TQueue<Item>& BC_TQueue<Item>::operator=(const BC_TQueue<Item>& q)
  27. {
  28.   if (this == &q)
  29.     return *this;
  30.   ((BC_TQueue<Item>&)q).Lock();
  31.   Purge();
  32.   BC_TQueueActiveIterator<Item> iter(q);
  33.   while (!iter.IsDone()) {
  34.     Add(*iter.CurrentItem());
  35.     iter.Next();
  36.   }
  37.   ((BC_TQueue<Item>&)q).Unlock();
  38.   return *this;
  39. }
  40.  
  41. template<class Item>
  42. BC_Boolean BC_TQueue<Item>::operator==(const BC_TQueue<Item>& q) const
  43. {
  44.   if (this == &q)
  45.     return 1;
  46.   ((BC_TQueue<Item>&)q).Lock();
  47.   if (Cardinality() != q.Cardinality()) {
  48.     ((BC_TQueue<Item>&)q).Unlock();
  49.     return 0;
  50.   }
  51.   BC_TQueueActiveIterator<Item> iter_1(*this), iter_2(q);
  52.   while (!iter_1.IsDone()) {
  53.     if (*iter_1.CurrentItem() != *iter_2.CurrentItem()) {
  54.       ((BC_TQueue<Item>&)q).Unlock();
  55.       return 0;
  56.     }
  57.     iter_1.Next();
  58.     iter_2.Next();
  59.   }
  60.   ((BC_TQueue<Item>&)q).Unlock();
  61.   return 1;
  62. }
  63.  
  64. template<class Item>
  65. BC_Boolean BC_TQueue<Item>::operator!=(const BC_TQueue<Item>& q) const
  66. {
  67.   return !operator==(q);
  68. }
  69.  
  70. template<class Item>
  71. void BC_TQueue<Item>::Lock() {}
  72.  
  73. template<class Item>
  74. void BC_TQueue<Item>::Unlock() {}
  75.  
  76. template<class Item>
  77. BC_TQueueActiveIterator<Item>::BC_TQueueActiveIterator(const BC_TQueue<Item>& q) 
  78.   : fQueue(q),
  79.     fIndex(q.Cardinality() ? 0 : -1) {}
  80.  
  81. template<class Item>
  82. BC_TQueueActiveIterator<Item>::~BC_TQueueActiveIterator() {}
  83.   
  84. template<class Item>
  85. void BC_TQueueActiveIterator<Item>::Reset()
  86. {
  87.   fIndex = fQueue.Cardinality() ? 0 : -1;
  88. }
  89.  
  90. template<class Item>
  91. BC_Boolean BC_TQueueActiveIterator<Item>::Next()
  92. {
  93.   fIndex++;
  94.   return !IsDone();
  95. }
  96.  
  97. template<class Item>
  98. BC_Boolean BC_TQueueActiveIterator<Item>::IsDone() const
  99. {
  100.   return ((fIndex < 0) || (fIndex >= fQueue.Cardinality()));
  101. }
  102.  
  103. template<class Item>
  104. const Item* BC_TQueueActiveIterator<Item>::CurrentItem() const
  105. {
  106.   return IsDone() ? 0 : &fQueue.ItemAt(fIndex);
  107. }
  108.  
  109. template<class Item>
  110. Item* BC_TQueueActiveIterator<Item>::CurrentItem()
  111. {
  112.   return IsDone() ? 0 : &((Item&)(fQueue.ItemAt(fIndex)));
  113. }
  114.  
  115. template<class Item>
  116. BC_TQueuePassiveIterator<Item>::BC_TQueuePassiveIterator(const BC_TQueue<Item>& q)
  117.   : fQueue(q) {}
  118.  
  119. template<class Item>
  120. BC_TQueuePassiveIterator<Item>::~BC_TQueuePassiveIterator() {}
  121.   
  122. template<class Item>
  123. BC_Boolean BC_TQueuePassiveIterator<Item>::Apply(BC_Boolean (*f)(const Item&))
  124. {
  125.   BC_TQueueActiveIterator<Item> iter(fQueue);
  126.   while (!iter.IsDone()) {
  127.     if (!f(*iter.CurrentItem()))
  128.       return 0;
  129.     else
  130.       iter.Next();
  131.   }
  132.   return 1;
  133. }
  134.   
  135. template<class Item>
  136. BC_Boolean BC_TQueuePassiveIterator<Item>::Apply(BC_Boolean (*f)(Item&))
  137. {
  138.   BC_TQueueActiveIterator<Item> iter(fQueue);
  139.   while (!iter.IsDone()) {
  140.     if (!f(*iter.CurrentItem()))
  141.       return 0;
  142.     else
  143.       iter.Next();
  144.   }
  145.   return 1;
  146. }
  147.